/* BMA180 Example

This is an example of using the BMA180 objext library

Typing 'a' returns the current acceleration values
Typing 's' returns the gain and bandwidth settings
Typing 'bn' sets the bandwidth to n, where n is ASCII 0 - 7
Typing 'gn' sets the gain to n, where n is ASCII 0 - 5

*/

#include <Wire.h>;


#include <BMA180.h>
BMA180 bma180;

int x_accel;
int y_accel;
int z_accel;


void setup() {
  // Join I2C Bus
  Wire.begin();
  // Initialize serial communication 
  Serial.begin(9600);
}

void loop() {
  
  
  if (Serial.available()) {
    char inByte = Serial.read();
    switch (inByte) {
      case 'a': {
        bma180.get_accel(&x_accel, &y_accel, &z_accel);
        Serial.print(x_accel);
        Serial.print(", ");
        Serial.print(y_accel);
        Serial.print(", ");
        Serial.println(z_accel);
        break;
      }
      case 's': {
        int temp = bma180.get_gain();
        Serial.print("gain = ");
        Serial.print(temp);
        temp = bma180.get_bw();
        Serial.print(", bw = ");
        Serial.println(temp);
        break;
      }
      case 'b': {
        while (!Serial.available()) {
          delay(10);
        }
        byte temp_bw = Serial.read() - 0x30;
        Serial.print("bw = ");
        Serial.println(temp_bw,HEX);
        if ((temp_bw >= 0) && (temp_bw < 10)) {
          bma180.set_bw(temp_bw);
          Serial.println("Set bw");
        }
        break;
      }
      case 'g': {
        while (!Serial.available()) {
          delay(10);
        }
        byte temp_g = Serial.read() - 0x30;
        Serial.print("g = ");
        Serial.println(temp_g,HEX);
        if ((temp_g >= 0) && (temp_g < 7)) {
          bma180.set_gain(temp_g);
        }
        break;
      }
    }
  }
}
